package com.cskaoyan.app.converter;

import com.cskaoyan.app.util.StringsUtil;
import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class StringToDateConverter implements Converter<String, Date> {
    @SneakyThrows
    @Override
    public Date convert(String source) {
        SimpleDateFormat simpleDateFormat = null;
        if (StringsUtil.isEmpty(source)) {
            return null;
        }
        if (source.length() == 19) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        }
        return simpleDateFormat.parse(source);
    }
}
